package com.guodidi.media.domain.bo;

import com.guodidi.media.domain.entity.Hls;
import lombok.Data;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * 通用配置 的ffmpeg配置 Created by 郭垚辉 on 2017/7/27.
 */
@Data
public class CommonParams {
    // 输入标志
    private String inputFlag = "-i";
    // 源文件的路径
    private Path sourcePath;

    // 虚构的生成文件的路径
    private Path virtualDestPath;
    // 真实的生成文件的路径
    private Path realDestPath;


    // 工作目录 workPath = virtualDestPath.resolve(相对路径)
    private Path workPath;
    // 输入视频的标志
    private String encodeVideoFlag = "-codec:v";
    // 视频编码
    private String encodeVideo;
    // 输入音频的标志
    private String encodeAudioFlag = "-codec:a";
    // 音频编码
    private String encodeAudio;
    // 线程数目输入的标志
    private String threadsFlag = "-threads";
    // 执行文件使用的线程数目，为0时，启动本机的所有线程
    private String threads;
    // 进行命令行的执行文件
    private String targetExe;
    // 最后生成的目标文件
    private Hls targetHls;
    // 子文件夹的路径 -- 不需要加文件分隔符
    private String hlsBaseUrl;

    // 默认构造器
    public CommonParams(String targetExe, Path sourcePath, Path destPath, String hlsBaseUrl) throws Exception {
        if (!destPath.isAbsolute()) {
            System.out.println("目的文件必须是一个绝对路径");
            throw new Exception("目的文件必须是一个绝对路径");
        }
        if (Files.notExists(sourcePath)) {
            throw new Exception("源文件不存在");
        }
        if (!Files.isRegularFile(sourcePath)) {
            throw new Exception("源文件必须是一个文件");
        }
        this.hlsBaseUrl = hlsBaseUrl;
        this.workPath = destPath.getParent();
        this.realDestPath = destPath;
        this.virtualDestPath = destPath;
        this.targetExe = targetExe;
        this.sourcePath = sourcePath;
        this.threads = "0";
        this.encodeAudio = "aac";
        this.encodeVideo = "libx264";
        this.targetHls = new Hls(destPath.getParent(), destPath);
    }

    public void addWorkPath(String path) {
        this.workPath = this.workPath.resolve(Paths.get(path));
    }

    public void setHlsBaseUrl(String hlsBaseUrl) {
        this.hlsBaseUrl = hlsBaseUrl + File.separator;
    }
}
